{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [2 3 4]]\n",
      "number of dim 2\n",
      "shape (2, 3)\n",
      "size 6\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 将普通的列表转换成numpy数组\n",
    "array = np.array([[1,2,3,],[2,3,4]])\n",
    "print(array)\n",
    "\n",
    "# numpy数组的维度(行) array.ndim\n",
    "print('number of dim', array.ndim)\n",
    "# numpy数组的形状（几行几列） array.shape\n",
    "print('shape', array.shape)\n",
    "# numpy数组一共有多少个元素 array.size\n",
    "print('size', array.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n",
      "[[ 1 23  4]\n",
      " [ 4 56  7]]\n",
      "[[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "[[1 1 1 1]\n",
      " [1 1 1 1]\n",
      " [1 1 1 1]]\n",
      "[[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "[10 12 14 16 18]\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[ 1.    3.25  5.5   7.75 10.  ]\n",
      "[[ 1.   2.8  4.6]\n",
      " [ 6.4  8.2 10. ]]\n"
     ]
    }
   ],
   "source": [
    "# 创建numpy数组\n",
    "# np.array(列表， dtype=数据类型)  数据类型（np.int, np.int32, np.int64, np.float, np.float32, np.float64）\n",
    "# 如果不写是几位的精度默认是32位\n",
    "\n",
    "import numpy as np\n",
    "a = np.array([1,23,4], dtype=np.int64)\n",
    "print(a.dtype)\n",
    "\n",
    "# 如果要定义一个二维矩阵\n",
    "b = np.array([[1,23,4],[4,56,7]])\n",
    "print(b)\n",
    "\n",
    "# 定义一个全部为0的矩阵np.zeros((几行,几列))\n",
    "c = np.zeros((3,4))\n",
    "print(c)\n",
    "\n",
    "# 定义一个全为1的矩阵np.ones((几行,几列), dtype=数据类型)\n",
    "d = np.ones((3,4), dtype=np.int)\n",
    "print(d)\n",
    "\n",
    "# 定义一个为空的矩阵np.empty((几行， 几列))  其实这个为空的矩阵每一个元素都是一个很接近0的数\n",
    "e = np.empty((3,4))\n",
    "print(e)\n",
    "\n",
    "# 定义一个连续的序列np.arange(起始值（默认为0），结束值(不包括)，步长（默认为1）)\n",
    "f = np.arange( 10, 20, 2)\n",
    "print(f)\n",
    "\n",
    "# 设置一个连续的序列，并且定义他的形状\n",
    "# np.arange(起始值（默认为0）， 结束值（不包括）， 步长（默认为1）).reshape((几行，几列))\n",
    "g = np.arange(12).reshape((3,4))\n",
    "print(g)\n",
    "\n",
    "# 生成线段，平均分段\n",
    "# np.linspace(起始值，结束值（包括），平均分成几段)\n",
    "h = np.linspace(1, 10, 5)\n",
    "print(h)\n",
    "# 也可以更改他的形状\n",
    "i = np.linspace(1, 10, 6).reshape((2,3))\n",
    "print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 20 30 40] [0 1 2 3]\n",
      "[10 19 28 37]\n",
      "[10 21 32 43]\n",
      "[  0  20  60 120]\n",
      "[0 1 4 9]\n",
      "[ True  True  True  True]\n",
      "[False False False  True]\n"
     ]
    }
   ],
   "source": [
    "# numpy的基本运算：就是每一个元素进行运算\n",
    "# 数组的运算\n",
    "import numpy as np\n",
    "a = np.array([10,20,30,40])\n",
    "b = np.arange(4)\n",
    "print(a, b)\n",
    "c = a - b  # 每个对应元素依次相减\n",
    "print(c)\n",
    "d = a + b  # 每个对应元素依次相加\n",
    "print(d)\n",
    "e = a * b  # 每个对应元素相乘  ，除法同理（只是后面一个数不能为0）\n",
    "print(e)  \n",
    "\n",
    "# 次方\n",
    "f = b ** 2;\n",
    "print(f) # 每一个相应的元素依次求次方\n",
    "\n",
    "# 判断数组内那些元素> 、 < 、 == 、 <= 、 >= 某个值的话，直接数组 比较运算符 比较的数字\n",
    "# 如：求b中是否有数字小于3，  返回值为True，or False\n",
    "print(b <= 3)\n",
    "print(b == 3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1]\n",
      " [0 3]]\n",
      "[[2 4]\n",
      " [2 3]]\n",
      "[[2 4]\n",
      " [2 3]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的运算\n",
    "\n",
    "import numpy as np\n",
    "a = np.array([[1,1],\n",
    "             [0,1]])\n",
    "b = np.arange(4).reshape((2,2))\n",
    "# print(a, b, sep='\\n')\n",
    "\n",
    "# 1、矩阵的每一个对应的元素相乘用*\n",
    "c = a * b\n",
    "print(c)\n",
    "\n",
    "# 2、矩阵的乘法运算（两种实现形式）\n",
    "# （1）np.dot(矩阵a,矩阵b)\n",
    "# （2）矩阵a.dot(矩阵b)  两者的计算结果是一样的\n",
    "c_dot_1 = np.dot(a, b)\n",
    "c_dot_2 = a.dot(b)\n",
    "print(c_dot_1)\n",
    "print(c_dot_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.51054817 0.4889865  0.18568442 0.15949355]\n",
      " [0.73302253 0.68461995 0.68462454 0.49988938]]\n",
      "3.9468690273311435\n",
      "[1.2435707  1.17360644 0.87030896 0.65938293]\n",
      "[1.34471264 2.60215639]\n"
     ]
    }
   ],
   "source": [
    "# 生成几行几列的(0~1)之间的随机元素的数组\n",
    "import numpy as py\n",
    "a = np.random.random((2,4))\n",
    "print(a)\n",
    "# 求最和\n",
    "# np.sum(矩阵a,axis=0/1) 如果不指定axis则是求整个矩阵的和\n",
    "# axis=1， 表示求每一行的和，返回一个（一行，几列）矩阵\n",
    "# axis=0， 表示求每一列的和，返回一个（一行，几列）矩阵\n",
    "print(np.sum(a))\n",
    "print(np.sum(a, axis=0))\n",
    "print(np.sum(a, axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵求最大值、最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.71113717 0.39817331 0.97584717 0.9409841 ]\n",
      " [0.54804028 0.10757302 0.35038348 0.17382065]]\n",
      "0.975847172517459\n",
      "[0.97584717 0.54804028]\n",
      "[0.71113717 0.39817331 0.97584717 0.9409841 ]\n",
      "0.10757301852105872\n",
      "[0.39817331 0.10757302]\n",
      "[0.54804028 0.10757302 0.35038348 0.17382065]\n"
     ]
    }
   ],
   "source": [
    "# 求最大值\n",
    "# np.max(矩阵a,axis=0/1) 如果不指定axis则是求整个矩阵的最大值\n",
    "# axis=1， 表示求每一行的最大值，返回一个（一行，几列）矩阵\n",
    "# axis=0， 表示求每一列的最大值，返回一个（一行，几列）矩阵\n",
    "import numpy as np\n",
    "a = np.random.random((2,4))\n",
    "print(a)\n",
    "print(np.max(a))\n",
    "print(np.max(a, axis=1))\n",
    "print(np.max(a, axis=0))\n",
    "# 求最小值\n",
    "# np.max(矩阵a,axis=0/1) 如果不指定axis则是求整个矩阵的最修小值\n",
    "# axis=1， 表示求每一行的最小值，返回一个（一行，几列）矩阵\n",
    "# axis=0， 表示求每一列的最小值，返回一个（一行，几列）矩阵\n",
    "print(np.min(a))\n",
    "print(np.min(a, axis=1))\n",
    "print(np.min(a, axis=0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 求矩阵最小值的索引(下标)（argmin（argument min）），最大值的索引（argmax（argument max））"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.arange(2,14).reshape((3,4))\n",
    "print(np.argmin(A))\n",
    "print(np.argmax(A))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 求矩阵的平均值    np.mean(矩阵a)    或者    矩阵a.mean()     或者np.average(矩阵a)(老版本)\n",
    "### 可以指定axis属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.5\n",
      "7.5\n",
      "7.5\n"
     ]
    }
   ],
   "source": [
    "print(A.mean())\n",
    "print(np.mean(A))\n",
    "print(np.average(A))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 求矩阵的中位数np.median(矩阵a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.5\n"
     ]
    }
   ],
   "source": [
    "print(np.median(A))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 求矩阵的累加和np.cumsum(矩阵a) \n",
    "### 求矩阵相邻两个元素之间的差值np.diff(矩阵a)\n",
    "### 求矩阵中非0的数，np.nonzero(矩阵a)，返回非0元素的下标\n",
    "### 对数组进行排序， np.sort(矩阵a)\n",
    "### 矩阵的转置 np.transpose(矩阵a)  或者  矩阵a.T\n",
    "### np.clip(矩阵a，最小值，最大值)--》将矩阵中的所有大于最大值的数变成最大值，所有小于最小值的数变成最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2  5  9 14 20 27 35 44 54 65 77 90]\n",
      "[[1 1 1]\n",
      " [1 1 1]\n",
      " [1 1 1]]\n",
      "(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]]\n",
      "[[ 2  6 10]\n",
      " [ 3  7 11]\n",
      " [ 4  8 12]\n",
      " [ 5  9 13]]\n",
      "[[ 2  6 10]\n",
      " [ 3  7 11]\n",
      " [ 4  8 12]\n",
      " [ 5  9 13]]\n",
      "矩阵的转置乘于矩阵\n",
      "[[140 158 176 194]\n",
      " [158 179 200 221]\n",
      " [176 200 224 248]\n",
      " [194 221 248 275]]\n",
      "[[5 5 5 5]\n",
      " [6 7 8 9]\n",
      " [9 9 9 9]]\n"
     ]
    }
   ],
   "source": [
    "print(np.cumsum(A))\n",
    "print(np.diff(A))\n",
    "print(np.nonzero(A))\n",
    "print(np.sort(A))\n",
    "print(np.transpose(A))\n",
    "print(A.T)\n",
    "\n",
    "print('矩阵的转置乘于矩阵')\n",
    "print((A.T).dot(A))\n",
    "print(np.clip(A, 5, 9))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### numpy索引\n",
    "### a：b：c   a表示开始元素的下标，b表示结束元素的下标（不包括），c表示步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 3  4  5  6]\n",
      " [ 7  8  9 10]\n",
      " [11 12 13 14]]\n",
      "第二行（从第0行开始）的数据 [11 12 13 14]\n",
      "第0行第0列的数据\n",
      "A[0][0]表示法 3\n",
      "A[0,0]表示法 3\n",
      "第0行的所有元素 [3 4 5 6]\n",
      "第0列的所有元素 [ 3  7 11]\n"
     ]
    }
   ],
   "source": [
    "#如矩阵A\n",
    "# 对于一维数组，A[0]-->表示取出下标为0的数组元素\n",
    "# 对于多维数组， A[2]-->表示取出第二行（从第0行开始）的元素\n",
    "# 对于多维数组， A[0][0]或者A[0,0] -->表示取出第0行第0列的元素\n",
    "# 用冒号代表所有数字，  如A[0,:] -->表示取出第0行的所有元素\n",
    "# A[:,0] -->表示取出第0列的所有元素\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "A = np.arange(3,15).reshape((3,4))\n",
    "print(A)\n",
    "print('第二行（从第0行开始）的数据', A[2])\n",
    "print('第0行第0列的数据')\n",
    "print('A[0][0]表示法', A[0][0])\n",
    "print('A[0,0]表示法', A[0,0] )\n",
    "print('第0行的所有元素', A[0,:])\n",
    "print('第0列的所有元素', A[:,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for循环是迭代行，如果要循环迭代一个数组的列的话，可以现将这个数组转置然后再进行行迭代（结果就是列迭代）\n",
    "### 把矩阵展开A.flatten()返回一个展开成一维数组的矩阵，A.flat返回一个展开成一维数组的迭代器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 5 6]\n",
      "[ 7  8  9 10]\n",
      "[11 12 13 14]\n",
      "[ 3  7 11]\n",
      "[ 4  8 12]\n",
      "[ 5  9 13]\n",
      "[ 6 10 14]\n",
      "展开成一维数组 [ 3  4  5  6  7  8  9 10 11 12 13 14]\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.arange(3,15).reshape((3,4))\n",
    "for row in A:\n",
    "    print(row)\n",
    "for column in A.T:\n",
    "    print(column)\n",
    "    \n",
    "#遍历矩阵的每一个元素\n",
    "print('展开成一维数组', A.flatten())\n",
    "for item in A.flat:\n",
    "    print(item)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
